package cn.sciento.farm.devicemanager.service.impl;

import cn.sciento.farm.devicemanager.dao.entity.DevInsectSituationLampLog;
import cn.sciento.farm.devicemanager.dao.mapper.DevInsectSituationLampLogMapper;
import cn.sciento.farm.devicemanager.service.DevInsectSituationLampLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Walton
 * @since 2020/9/8 9:51
 */
@Slf4j
@Service
public class DevInsectSituationLampLogServiceImpl implements DevInsectSituationLampLogService {

    @Autowired
    DevInsectSituationLampLogMapper insectSituationLampLogMapper;


    @Override
    public int insert(DevInsectSituationLampLog insectLog) {
        return insectSituationLampLogMapper.insert(insectLog);
    }

    @Scheduled(cron = "0 0 0 1 1/2 ?")
    public void batchDeleteLog(){
        Date monthBefore = getTwoMonthBefore(new Date());
        long time = monthBefore.getTime();
        int result = insectSituationLampLogMapper.batchDeleteLogByCreateTime(time);
        log.info("已删除当前时间两个月前的"+ result+"条日志");
    }

    // 获取当前日期2个月前的时间
    public Date getTwoMonthBefore(Date date){
        // 获取当前时间
        Calendar c = Calendar.getInstance(); //得到日历
        c.setTime(date);//把当前时间赋给日历
        c.add(c.MONTH, -2); //设置为前2月，可根据需求进行修改
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 00);
        c.set(Calendar.MINUTE, 00);
        c.set(Calendar.SECOND, 00);
        return c.getTime();
    }

}
